package com.lecture.java8.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @program: spring-lecture
 * @description:
 *
 *          java8四大函数式接口
 *          Consumer<T>：消费型接口
 *              void accept(T t);
 *
 *           Supplier<T>供给型接口
 *              T get();                   . . . .
 *
 *           Function<T, R>：函数型接口
 *              R apply(T t);
 *           Predicate<T>：断言型接口
 *              boolean test(T t);
 *
 *
 * @author: luyn
 * @create: 2020-03-26 20:16
 **/
public class TestLambda3 {
    public static void main(String[] args) {
        // Consumer<T> 消费性接口
        TestLambda3 testLambda3 = new TestLambda3();
        testLambda3.happy(300, (m) -> {
            System.out.println("消费了：" + m);
        });
        double d = 0.3;
        testLambda3.happy(d, (item) -> {
            System.out.println(item + 10);
        });

        testLambda3.happy2(12, () -> Math.random());

        String s = "sbD";
        String str = testLambda3.function(s, String::toUpperCase);
        System.out.println(str);
    }

    public void happy(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    public List<Double> happy2(double money, Supplier<Double> supplier) {
        List<Double> list = new ArrayList<>();
        for (int i = 0; i < money; i ++) {
            list.add(supplier.get());
        }
        return list;
    }

    public void ha(double m, Consumer<Double> consumer) {
        consumer.accept(m);
    }

    public String function(String str, Function<String, String> function) {
        return function.apply(str);
    }
}
